﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class NhanVienDAO
    {
        public int DangNhap(string MaNV, string Pass)
        {
            int kq = 0;
            try
            {
                SqlConnection conn = new SqlConnection();
                conn = DataProvider.ConnectDB();

                SqlCommand cmd = new SqlCommand("proc_DangNhap",conn);
                cmd.CommandType = CommandType.StoredProcedure;
               // cmd.Connection = conn;

                SqlParameter sqlPar;
                sqlPar = new SqlParameter("manv", SqlDbType.VarChar, 6);
                sqlPar.Direction = ParameterDirection.Input;
                sqlPar.Value =MaNV;
                cmd.Parameters.Add(sqlPar);
                sqlPar = new SqlParameter("pass", SqlDbType.Char, 10);
                sqlPar.Direction = ParameterDirection.Input;
                sqlPar.Value = Pass;
                cmd.Parameters.Add(sqlPar);
                sqlPar = new SqlParameter("res", SqlDbType.Int);
                sqlPar.Direction = ParameterDirection.Output;
                cmd.Parameters.Add(sqlPar);


                kq=cmd.ExecuteNonQuery();
                kq = int.Parse(cmd.Parameters["res"].Value.ToString());
                conn.Close();
            }
            catch(SqlException ex)
            {
                return kq;
            }
            return kq;
        }
        
        public NhanVienDTO LayThongTinNV (string MaNV)
        {
            NhanVienDTO nv = new NhanVienDTO();
            SqlConnection conn = new SqlConnection();
            conn = DataProvider.ConnectDB();

            SqlCommand cmd = new SqlCommand("proc_LayThongTinNV", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter sqlPar;
            sqlPar = new SqlParameter("manv", SqlDbType.VarChar, 6);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = MaNV;
            cmd.Parameters.Add(sqlPar);
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
                nv.MaNV = reader.GetString(0);
                nv.Pass = reader.GetString(1);
                nv.TenNV = reader.GetString(2);
                nv.LoaiNV = reader.GetInt32(3);
            
            conn.Close();
            return nv;
        }
        public List<string> LayMaNV()
        {
            List<string> lis = new List<string>();
            string sql = "SELECT MaNV FROM NHANVIEN";
            SqlConnection conn = new SqlConnection();
            conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                lis.Add(reader.GetString(0));
            }
            conn.Close();
            return lis;
        }
    }
}
